@@ -2,11 +2,9 @@ package com.example.skycode7.dao;

import com.example.skycode7.BEAN.NewsBean;
import com.example.skycode7.utill.DBConnection;
import com.example.skycode7.utill.Time;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@@ -30,7 +28,7 @@ public class NewsDao {
            //将新闻数据对象中数据依次绑定到sql命令中
            ps.setString(1, news.getNewsTitle());
            ps.setString(2, news.getNewsContent());
            ps.setString(3, news.getNewsTime());
            ps.setString(3, Time.grtDateTime());
            ps.setString(4, news.getAdminName());
            return ps.executeUpdate();//执行sql命令 完成数据插入
        } catch (SQLException e) {
@@ -73,7 +71,7 @@ public class NewsDao {
            //将新闻数据对象中数据依次绑定到sql命令中
            ps.setString(1, news.getNewsTitle());
            ps.setString(2, news.getNewsContent());
            ps.setString(3, news.getNewsTime());
            ps.setString(3, Time.grtDateTime());
            ps.setString(4, news.getAdminName());
            ps.setInt(5, news.getNewsID());
            return ps.executeUpdate();//执行sql命令 完成数据插入
@@ -114,5 +112,41 @@ public class NewsDao {
        }
        return list;//异常情况返回空集合
    }

    public int count() {
        Connection conn = db.getConn();
        String sql = "select count(*) from news";
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    public NewsBean getOne(int id) {
        Connection conn = db.getConn();
        String sql = "select * from news where NewsID = ?";
        NewsBean news = new NewsBean();
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1,id);
            ResultSet rs = pmst.executeQuery();
            if (rs.next()){
                news.setNewsID(rs.getInt("NewsID"));
                news.setNewsTitle(rs.getString("NewsTitle"));
                news.setNewsContent(rs.getString("NewsContent"));
                news.setNewsTime(rs.getString("NewsTime"));
                news.setAdminName(rs.getString("AdminName"));
            }
        } catch(SQLException e){
            throw new RuntimeException(e);
        }
        return news;
        }
}
